﻿using MediatR;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.AdmissionRegistrationFile
{
    /// <summary>
    /// 进场登记显示
    /// </summary>
    public class ApproachListHander : IRequestHandler<ApproachListCommand, PageDTO<Approach>>
    {
        private readonly IBateRepository<Approach> approach;

        public ApproachListHander(IBateRepository<Approach> approach)
        {
            this.approach = approach;
        }

        public async Task<PageDTO<Approach>> Handle(ApproachListCommand request, CancellationToken cancellationToken)
        {
            try
            {
                PageDTO<Approach> res = new PageDTO<Approach>() { pageCount = 0,totalCount=0 };
                var list= approach.GetAll();
                if (request.ApproachTime != null)
                {
                    list = list.Where(x => x.ApproachTime == request.ApproachTime);
                }
                if (!string.IsNullOrEmpty(request.Suppliers))
                {
                    list = list.Where(x => x.Suppliers.Contains(request.Suppliers));
                }
                if (!string.IsNullOrEmpty(request.SettlementType))
                {
                    list = list.Where(x => x.SettlementType.Contains(request.SettlementType));
                }
                if (request.ApproachStatus > -1)
                {
                    list = list.Where(x => x.ApproachStatus==request.ApproachStatus);
                }
                res.totalCount=list.Count();
                res.pageCount = (int)Math.Ceiling(res.totalCount * 1.0 / request.pageSize);
                list = list.OrderBy(x => x.ApproachId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize);
                res.Lists = list.ToList();
                return res; 
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
